﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SetSailModel;
using System.Net;
using System.Threading;
using System.IO.Compression;
using ICSharpCode.SharpZipLib.Zip;
using SetSailDal;
using System.Collections;
using static SetSailReadFileTools.Common;

namespace SetSailReadFileTools
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(((System.ComponentModel.Component)(this)));
            this.skinEngine1.SkinFile = Application.StartupPath + "//RealOne.ssk";
        }
        //我的文档欧卡车队联运存档
        string MyFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Euro Truck Simulator 2\\profiles";
        string MyFilePath2 = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Euro Truck Simulator 2";
        string PathFileName = Properties.Settings.Default.PathFileName_Q;//获取存档文件名（全套）
        string PathFileName2 = Properties.Settings.Default.PathFileName_B;//获取存档文件名(本体)

        private void Main_Load(object sender, EventArgs e)
        {

            //检测前缀名是否更改
            //Hashtable para = new Hashtable();
            //object obj = DecryptObject(Environment.GetFolderPath(Environment.SpecialFolder.Personal)+ "\\ETS2MP\\config.dat");
            //para = obj as Hashtable;
            //string MP2 = para["MP2"].ToString();

            comboBox4.SelectedIndex = 0;
            comboBox1.SelectedIndex = 0;
            label27.ForeColor = Color.FromArgb(192, 0, 0);
            xButton1.Enabled = false;
            xButton2.Enabled = false;

            try
            {
                label9.ForeColor = Color.Blue;
                LH_YHXX ly = CookieData.getInstance().YHXX;
                if (ly != null)
                {
                    if (!ly.ZHLX.Contains("管理员"))
                    {
                        tabPage2.Parent = null;
                        tabPage3.Parent = null;
                        tabPage4.Parent = null;
                        tabPage5.Parent = null;
                        tabPage6.Parent = null;
                        tabPage7.Parent = null;
                        tabPage9.Parent = null;
                        dataGridViewTextBoxColumn8.ReadOnly = true;
                    }
                    else if (ly.ZHLX == "超级管理员")
                    {
                        dataGridViewTextBoxColumn8.ReadOnly = false;
                    }
                }
                //读档类型
                comboBox5.SelectedIndex = 0;

                //存档类型
                comboBox6.SelectedIndex = 0;
                //存档记录
                textBox11.Text = "1";
                textBox10.Text = "0";
                comboBox2.Text = "15";

                //读档记录
                textBox12.Text = "1";
                textBox13.Text = "0";
                comboBox3.Text = "15";

                //读取前缀名
                Result<LH_CDQZM> qzm = MainDal.Get_QZM();
                if (qzm.Code == ResultCode.Success)
                {
                    textBox3.Text = qzm.Data.QZM.Trim();
                }
                else
                {
                    MessageBox.Show(qzm.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 关闭退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        #region 启航者车队读档

        /// <summary>
        /// 获取服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton3_Click(object sender, EventArgs e)
        {
            bool flag = false;
            string[] arr = FTPGetFileList("ftp://146.56.250.94", "ftp_setsail", "@synca123.");
            foreach (var item in arr)
            {
                string filename = DateTime.Today.ToString("yyyy-MM-dd");
                if (item.Contains(filename))
                {
                    textBox2.Text = item;
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                xButton1.HoverBackColor = Color.CornflowerBlue;
                xButton1.Enabled = true;
            }
            else
            {
                MessageBox.Show("今日存档未上传");
            }
        }

        /// <summary>
        /// 获取存档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(textBox2.Text))
                {
                    MessageBox.Show("请先获取服务器");
                    return;
                }
                //验证文件夹是否存在
                if (!Directory.Exists(MyFilePath))
                {
                    MessageBox.Show("请先安装游戏");
                    return;
                }
                bool flag = false;
                string filename = DateTime.Today.ToString("yyyy-MM-dd");
                if (comboBox5.Text != "全套")
                    filename += "_local";
                string[] arr = FTPGetFileList("ftp://146.56.250.94/" + filename, "ftp_setsail", "@synca123.");
                foreach (var item in arr)
                {
                    if (item.Split('/')[0] == filename)
                    {
                        Thread WaitingThread = new Thread(new ThreadStart(() =>
                        {
                            WaitFun _wf = new WaitFun();
                            _wf.BringToFront();
                            _wf.StartPosition = FormStartPosition.CenterParent;
                            _wf.ShowDialog();
                        }));
                        WaitingThread.Start();
                        string str = FTPDownloadFile("ftp://146.56.250.94/" + item, "ftp_setsail", "@synca123.", MyFilePath, item.Split('/')[1]);
                        WaitingThread.Abort();
                        flag = true;
                        xButton1.Enabled = false;
                        xButton2.Enabled = true;
                        Thread.Sleep(1000);
                        MessageBox.Show(str);
                        break;
                    }
                }
                if (!flag)
                    MessageBox.Show("存档获取失败");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 同步存档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton2_Click(object sender, EventArgs e)
        {
            try
            {
                string path = MyFilePath + "\\" + PathFileName + ".zip";
                if (comboBox5.Text != "全套")
                    path = MyFilePath + "\\" + PathFileName2 + ".zip";

                //验证文件是否存在
                if (System.IO.File.Exists(path))
                {
                    string error = "";
                    string filename = MyFilePath + "\\" + PathFileName;
                    if (comboBox6.Text != "全套")
                        filename = MyFilePath + "\\" + PathFileName2;
                    //验证当前路径下是否存在save目录
                    if (Directory.Exists(filename + "\\save"))
                    {
                        string err = DeleteDirectory(filename, "save");
                        if (!string.IsNullOrWhiteSpace(err))
                        {
                            MessageBox.Show(err);
                            return;
                        }
                    }
                    bool flag = ZipHelper.UnZipFile(path, MyFilePath, out error);

                    if (flag)
                    {
                        // 2、根据路径字符串判断是文件还是文件夹
                        FileAttributes attr = File.GetAttributes(path);
                        // 3、根据具体类型进行删除
                        if (attr == FileAttributes.Directory)
                        {
                            // 3.1、删除文件夹
                            Directory.Delete(path, true);
                        }
                        else
                        {
                            // 3.2、删除文件
                            File.Delete(path);
                        }
                    }
                    else
                    {
                        MessageBox.Show(error);
                    }
                }
                else
                {
                    MessageBox.Show("请先获取文档");
                    return;
                }

                //保存读档记录
                var result = MainDal.Save_DDJL(CookieData.getInstance().YHXX.DLZH);
                if (result.Code == ResultCode.Success)
                {
                    MessageBox.Show("存档已同步，尽情耍吧");
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 飞行模式设置——保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton4_Click(object sender, EventArgs e)
        {
            try
            {
                int is_open = radioButton1.Checked ? 1 : 0;
                if (Directory.Exists(MyFilePath2))
                {

                    //删除steam云存档
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Euro Truck Simulator 2\\";
                    DeleteDirectoryOrFlie(path, "steam_profiles");

                    //修改飞行模式存档

                    string read = File.ReadAllText(MyFilePath2 + "\\config.cfg");

                    if (is_open == 1)
                    {
                        read.Replace("uset g_developer \"0\"", "uset g_developer \"1\"").Replace("uset g_console \"0\"", "uset g_console \"1\"");
                    }
                    else
                    {
                        read.Replace("uset g_developer \"1\"", "uset g_developer \"0\"").Replace("uset g_console \"1\"", "uset g_console \"0\"");
                    }

                    File.WriteAllText(MyFilePath2 + "\\config.cfg",read);
                    MessageBox.Show("设置成功，请重新登录游戏");
                }
                else
                {
                    MessageBox.Show("路径不存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 飞行模式groupbox边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>+
        private void groupBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox1.BackColor);
            e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, Brushes.Black, 10, 1);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 8, 7);
            e.Graphics.DrawLine(Pens.Black, e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 8, 7, groupBox1.Width - 2, 7);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 1, groupBox1.Height - 2);
            e.Graphics.DrawLine(Pens.Black, 1, groupBox1.Height - 2, groupBox1.Width - 2, groupBox1.Height - 2);
            e.Graphics.DrawLine(Pens.Black, groupBox1.Width - 2, 7, groupBox1.Width - 2, groupBox1.Height - 2);
        }

        /// <summary>
        /// 设置车队前缀名groupbox边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupBox2_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox2.BackColor);
            e.Graphics.DrawString(groupBox2.Text, groupBox2.Font, Brushes.Black, 10, 1);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 8, 7);
            e.Graphics.DrawLine(Pens.Black, e.Graphics.MeasureString(groupBox2.Text, groupBox2.Font).Width + 8, 7, groupBox2.Width - 2, 7);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 1, groupBox2.Height - 2);
            e.Graphics.DrawLine(Pens.Black, 1, groupBox2.Height - 2, groupBox2.Width - 2, groupBox2.Height - 2);
            e.Graphics.DrawLine(Pens.Black, groupBox2.Width - 2, 7, groupBox2.Width - 2, groupBox2.Height - 2);
        }

        /// <summary>
        /// 复制前缀名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton5_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();//清空粘贴板信息
            Clipboard.SetData(DataFormats.Text, textBox3.Text.Trim());//往系统粘贴板设置数据
        }

        /// <summary>
        /// 同步文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton23_Click(object sender, EventArgs e)
        {
            MessageBox.Show("功能暂未开发");
        }

        /// <summary>
        /// 设置前缀名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton6_Click(object sender, EventArgs e)
        {
            if (CookieData.getInstance().YHXX.ZHLX == "队员")
            {
                MessageBox.Show("你没有权限设置");
                return;
            }
            textBox3.Text = "";
            textBox3.ReadOnly = false;
        }

        /// <summary>
        /// 保存前缀名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton7_Click(object sender, EventArgs e)
        {
            if (CookieData.getInstance().YHXX.ZHLX == "队员")
            {
                MessageBox.Show("你没有权限保存");
                return;
            }
            var result = MainDal.Set_QZM(textBox3.Text.Trim());
            if (result.Code == ResultCode.Success)
            {
                MessageBox.Show("前缀名已保存");
                textBox3.ReadOnly = true;
            }
            else
            {
                MessageBox.Show("前缀名保存失败");
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_i_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region 启航者车队存档

        /// <summary>
        /// 文件信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupBox3_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox3.BackColor);
            e.Graphics.DrawString(groupBox3.Text, groupBox3.Font, Brushes.Black, 10, 1);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 8, 7);
            e.Graphics.DrawLine(Pens.Black, e.Graphics.MeasureString(groupBox3.Text, groupBox3.Font).Width + 8, 7, groupBox3.Width - 2, 7);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 1, groupBox3.Height - 2);
            e.Graphics.DrawLine(Pens.Black, 1, groupBox3.Height - 2, groupBox3.Width - 2, groupBox3.Height - 2);
            e.Graphics.DrawLine(Pens.Black, groupBox3.Width - 2, 7, groupBox3.Width - 2, groupBox3.Height - 2);
        }

        /// <summary>
        /// 设置存档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton10_Click(object sender, EventArgs e)
        {
            //验证文件夹是否存在

            string pathfile = PathFileName;
            if (comboBox6.Text != "全套")
                pathfile = PathFileName2;
            string path = MyFilePath + "\\" + pathfile;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("未查找到车队存档");
                return;
            }
            ZipHelper.CreateZip(MyFilePath, path + ".zip", pathfile);
            textBox4.Text = path;

            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("UpdDate", typeof(string));
            dt.Columns.Add("Size", typeof(string));

            string[] arr = Directory.GetFiles(path);

            foreach (var item in arr)
            {
                FileInfo fi = new FileInfo(item);
                DataRow row = dt.NewRow();
                row["Name"] = fi.Name.Split('.')[0];
                row["Type"] = fi.Extension.Replace(".", "");
                row["UpdDate"] = fi.LastAccessTime;
                row["Size"] = Convert.ToInt32(fi.Length / 1024) + "KB";
                dt.Rows.Add(row);
            }
            dataGridView1.DataSource = dt;

            MessageBox.Show("已设置存档，请上传到云端");
        }

        /// <summary>
        /// 上传存档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton9_Click(object sender, EventArgs e)
        {
            try
            {
                string path = MyFilePath + "\\" + PathFileName + ".zip";
                if (comboBox6.Text != "全套")
                    path = MyFilePath + "\\" + PathFileName2 + ".zip";
                if (!File.Exists(path))
                {
                    MessageBox.Show("请先设置存档");
                    return;
                }
                string filename = DateTime.Today.ToString("yyyy-MM-dd");
                if (comboBox6.Text != "全套")
                    filename += "_local";
                string[] arr = FTPGetFileList("ftp://146.56.250.94", "ftp_setsail", "@synca123.");
                foreach (var item in arr)
                {
                    //若已存在当前路径，删除目录重新上传
                    if (item == filename)
                    {
                        FTPDeleteFile(filename, "ftp://146.56.250.94", "ftp_setsail", "@synca123.");
                        break;
                    }
                }

                string str = CreateDir(filename, "ftp://146.56.250.94/", "ftp_setsail", "@synca123.");

                //压缩包上传到服务器
                Thread WaitingThread = new Thread(new ThreadStart(() =>
                {
                    WaitFun _wf = new WaitFun("正在上传...请稍后");
                    _wf.BringToFront();
                    _wf.StartPosition = FormStartPosition.CenterParent;
                    _wf.ShowDialog();
                }));
                WaitingThread.Start();
                FTPUploadFile("ftp://146.56.250.94/" + filename, "ftp_setsail", "@synca123.", path);
                WaitingThread.Abort();

                //删除文件夹
                File.Delete(path);

                //保存存档记录
                var result = MainDal.Save_CDJL(CookieData.getInstance().YHXX.DLZH);
                if (result.Code == ResultCode.Success)
                {
                    MessageBox.Show("联运存档已上传,请开始游戏");
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region 修改密码

        /// <summary>
        /// 保存修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton24_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox14.Text.Trim()))
            {
                MessageBox.Show("请输入确认密码");
                return;
            }

            if (textBox14.Text.Trim().Length < 8)
            {
                MessageBox.Show("密码不能低于八位");
                return;
            }

            if (string.IsNullOrWhiteSpace(textBox15.Text.Trim()))
            {
                MessageBox.Show("请输入确认密码");
                return;
            }
            if (textBox14.Text.Trim() != textBox15.Text.Trim())
            {
                MessageBox.Show("两次密码输入不一致");
                return;
            }


            var result = MainDal.Upd_PWD(MD5Encrypt(textBox14.Text.Trim()), CookieData.getInstance().YHXX.ID);
            if (result.Code == ResultCode.Success)
            {
                textBox14.Text = "";
                textBox15.Text = "";
                MessageBox.Show("密码修改成功");
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }

        #endregion

        #region 后台审核

        /// <summary>
        /// 后台审核查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton12_Click(object sender, EventArgs e)
        {
            var result = MainDal.Query_HTSH(textBox5.Text.Trim());
            if (result.Code == ResultCode.Success)
            {
                dataGridView2.DataSource = result.Data;
            }
        }

        /// <summary>
        /// 审核操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView2.Columns[e.ColumnIndex].DataPropertyName == "SHTG")
            {
                LH_ZCSH lz = new LH_ZCSH();
                lz.ID = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
                lz.ZCZH = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
                lz.ZCMM = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
                lz.YY = dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString();
                lz.QQ = dataGridView2.Rows[e.RowIndex].Cells[4].Value.ToString();
                lz.SHZH = CookieData.getInstance().YHXX.DLZH;
                lz.SHZT = "通过";
                var result = MainDal.QDSH_HTSH(lz);
                if (result.Code == ResultCode.Success)
                {
                    xButton12_Click(null, null);
                    MessageBox.Show("已审核通过");
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
            else if (dataGridView2.Columns[e.ColumnIndex].DataPropertyName == "SHBTG")
            {
                LH_ZCSH lz = new LH_ZCSH();
                lz.ID = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
                lz.SHZH = CookieData.getInstance().YHXX.DLZH;
                lz.SHZT = "不通过";
                var result = MainDal.QDSH_HTSH(lz);
                if (result.Code == ResultCode.Success)
                {
                    xButton12_Click(null, null);
                    MessageBox.Show("已审核不通过");
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
        }

        #endregion

        #region 审核记录
        /// <summary>
        /// 审核记录 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton13_Click(object sender, EventArgs e)
        {
            var result = MainDal.Query_SHJL(textBox6.Text.Trim(), comboBox4.Text);
            if (result.Code == ResultCode.Success)
            {
                dataGridView3.DataSource = result.Data;
            }
        }
        #endregion

        #region 用户管理

        /// <summary>
        /// 用户管理查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton14_Click(object sender, EventArgs e)
        {
            var result = MainDal.Query_YHXX(textBox7.Text.Trim(), comboBox1.Text);
            if (result.Code == ResultCode.Success)
            {
                dataGridView4.DataSource = result.Data;
            }
        }

        ComboBox cbo = new ComboBox();
        private void dataGridView4_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView4.CurrentCell.OwningColumn.DataPropertyName == "ZHLX")
            {
                cbo = e.Control as ComboBox; //保存当前的事件源。为了触发事件后。在取消
                cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged);
            }
        }

        void cbo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combox = sender as ComboBox;

            var result = MainDal.Update_ZHLX(dataGridView4.Rows[dataGridView4.CurrentRow.Index].Cells[0].Value.ToString(), combox.Text);
            if (result.Code == ResultCode.Success)
            {
                MessageBox.Show(result.Message);
            }
            else
            {
                MessageBox.Show(result.Message);
            }
            //combox.Text和cbo.Text获取的值是相同的

            //做完处理，须撤销动态事件。如果不撤销会遇到什么问题。你可以自己试试
            cbo.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
            //或者
            combox.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged);
        }

        /// <summary>
        /// 实现单击一次显示下拉列表框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView4_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //
            if (dataGridView4.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1)
            {
                SendKeys.Send("{F4}");
            }
        }

        /// <summary>
        /// 行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView4.Columns[e.ColumnIndex].DataPropertyName == "JZDL")
            {
                if (dataGridView4.Rows[e.RowIndex].Cells[6].Value.ToString() == "禁止登录")
                {
                    MessageBox.Show("该账号已禁止登录");
                    return;
                }
                var result = MainDal.Upd_JZDL(dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString(), "禁止登录");
                if (result.Code == ResultCode.Success)
                {
                    xButton14_Click(null, null);
                    MessageBox.Show("已禁止登录");
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
            else if (dataGridView4.Columns[e.ColumnIndex].DataPropertyName == "HFDL")
            {
                if (dataGridView4.Rows[e.RowIndex].Cells[6].Value.ToString() == "正常登录")
                {
                    MessageBox.Show("该账号已正常登录");
                    return;
                }
                var result = MainDal.Upd_JZDL(dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString(), "正常登录");
                if (result.Code == ResultCode.Success)
                {
                    xButton14_Click(null, null);
                    MessageBox.Show("已正常登录");
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
        }
        #endregion

        #region 存档记录

        private void GetData_CDJL()
        {
            if (Convert.ToInt32(textBox11.Text) > Convert.ToInt32(textBox10.Text) && textBox10.Text != "0")
                textBox11.Text = textBox10.Text;
            var result = MainDal.Query_CDJL(Convert.ToInt32(textBox11.Text) - 1, Convert.ToInt32(comboBox2.Text), textBox8.Text.Trim());
            if (result.Code == ResultCode.Success)
            {
                dataGridView5.DataSource = result.Data;
                textBox10.Text = result.Count.ToString();
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }

        /// <summary>
        /// 只允许输入正整数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox11_KeyPress(object sender, KeyPressEventArgs e)
        {
            int len = textBox11.Text.Length;

            if (len <= 1 && e.KeyChar == '0')
            {
                e.Handled = true;
            }
            else if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }
            else
            {
                if (len <= 1)
                {
                    e.Handled = true;
                }
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton15_Click(object sender, EventArgs e)
        {
            GetData_CDJL();
        }

        /// <summary>
        /// 查询行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox11.Text = "1";
            GetData_CDJL();
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton19_Click(object sender, EventArgs e)
        {
            GetData_CDJL();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton17_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textBox11.Text) > 1)
            {
                textBox11.Text = (Convert.ToInt32(textBox11.Text) - 1).ToString();
                GetData_CDJL();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton18_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textBox11.Text) < Convert.ToInt32(textBox10.Text))
            {
                textBox11.Text = (Convert.ToInt32(textBox11.Text) + 1).ToString();
                GetData_CDJL();
            }
        }

        /// <summary>
        /// 删除存档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView5_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView5.Columns[e.ColumnIndex].DataPropertyName == "SCCD")
            {
                DialogResult dialog = MessageBox.Show("确实要删除记录吗？", "提示", MessageBoxButtons.YesNo);
                if (dialog == DialogResult.Yes)
                {
                    var result = MainDal.Upd_CDJL(dataGridView5.Rows[e.RowIndex].Cells[1].Value.ToString());
                    if (result.Code == ResultCode.Success)
                    {
                        xButton14_Click(null, null);
                        textBox11.Text = "1";
                        GetData_CDJL();
                        MessageBox.Show("已删除记录");
                    }
                    else
                    {
                        MessageBox.Show(result.Message);
                    }
                }
            }
        }
        #endregion

        #region 读档记录
        private void GetData_DDJL()
        {
            if (Convert.ToInt32(textBox12.Text) > Convert.ToInt32(textBox13.Text) && textBox13.Text != "0")
                textBox12.Text = textBox13.Text;
            var result = MainDal.Query_DDJL(Convert.ToInt32(textBox12.Text) - 1, Convert.ToInt32(comboBox3.Text), textBox9.Text.Trim());
            if (result.Code == ResultCode.Success)
            {
                dataGridView6.DataSource = result.Data;
                textBox13.Text = result.Count.ToString();
            }
            else
            {
                MessageBox.Show(result.Message);
            }
        }

        /// <summary>
        /// 只允许输入正整数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)
        {
            int len = textBox12.Text.Length;

            if (len <= 1 && e.KeyChar == '0')
            {
                e.Handled = true;
            }
            else if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }
            else
            {
                if (len <= 1)
                {
                    e.Handled = true;
                }
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton16_Click(object sender, EventArgs e)
        {
            GetData_DDJL();
        }

        /// <summary>
        /// 查询行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox12.Text = "1";
            GetData_DDJL();
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton21_Click(object sender, EventArgs e)
        {
            GetData_DDJL();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton22_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textBox12.Text) > 1)
            {
                textBox12.Text = (Convert.ToInt32(textBox12.Text) - 1).ToString();
                GetData_DDJL();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xButton20_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textBox12.Text) < Convert.ToInt32(textBox13.Text))
            {
                textBox12.Text = (Convert.ToInt32(textBox12.Text) + 1).ToString();
                GetData_DDJL();
            }
        }

        /// <summary>
        /// 删除读档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView6_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView6.Columns[e.ColumnIndex].DataPropertyName == "SCDD")
            {
                DialogResult dialog = MessageBox.Show("确实要删除记录吗？", "提示", MessageBoxButtons.YesNo);
                if (dialog == DialogResult.Yes)
                {
                    var result = MainDal.Upd_DDJL(dataGridView6.Rows[e.RowIndex].Cells[1].Value.ToString());
                    if (result.Code == ResultCode.Success)
                    {
                        xButton14_Click(null, null);
                        textBox12.Text = "1";
                        GetData_DDJL();
                        MessageBox.Show("已删除记录");
                    }
                    else
                    {
                        MessageBox.Show(result.Message);
                    }
                }
            }
        }
        #endregion

        #region 软件升级

        #endregion

        bool flag = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (flag)
            {
                label1.ForeColor = Color.Brown;
                flag = false;
            }
            else
            {
                label1.ForeColor = Color.Blue;
                flag = true;
            }
        }
    }
}
